/*
* 文 件 名:  AbsAction.java
* 版    权:  Tydic Technologies Co., Ltd. Copyright 1993-2013,  All rights reserved
* 描    述:  <描述>
* 修 改 人:  Administrator
* 修改时间:  2013-6-17
* 跟踪单号:  <需求跟踪单号>
* 修改单号:  <需求修改单号>
* 修改内容:  <修改内容>
*/
package com.hfut.jpoa.common.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

/**
* action基类
* <功能详细描述>
* 
* @author  yangjw1
* @version  [1.0, 2013-6-17]
* @since  [产品中心/1.0]
*/
public class AbsAction<T> extends ActionSupport {
    
    private static final long serialVersionUID = -3778439797752073513L;
    
    public static final Logger LOGGER = LoggerFactory.getLogger(AbsAction.class);
    
    /**
     * 
     * 成功操作
     * 异步调用返回成功结果给页面
     * @param data 返回给页面的数据
     * @throws IOException 输入输出异常
     */
    public void success(Object data)
        throws IOException {
        LOGGER.info("sucess:" + data);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag", "success");
        result.put("data", data);
        ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
        ServletActionContext.getResponse().getWriter().write(JSONObject.toJSONString(result));
        ServletActionContext.getResponse().getWriter().close();
    }
    
    /**
     * 
     * 失败操作
     * 异步调用返回失败结果给页面
     * @param errorMsg 错误信息
     * @throws IOException 输入输出异常
     */
    public void failure(String errorMsg)
        throws IOException {
        LOGGER.info("failure:" + errorMsg);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag", "error");
        result.put("errorMsg", errorMsg);
        ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
        ServletActionContext.getResponse().getWriter().write(JSONObject.toJSONString(result));
        ServletActionContext.getResponse().getWriter().close();
    }
    
    public String sendMessages(String json)
        throws Exception {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("application/json");
        response.setContentType("text/json; charset=utf-8");
        
        response.setCharacterEncoding("UTF-8");
        response.getWriter().print(json);
        return null;
    }
   
}
